<?php
namespace iBrand\Shop\Core\Processor;

use Carbon\Carbon;
use iBrand\Component\Order\Models\Order;
use iBrand\Shop\Core\Models\ShopOrder;
use Illuminate\Contracts\Events\Dispatcher;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/7
 * Time: 12:46
 */
class ShopOrderProcessor
{
    protected $event;

    public function __construct(Dispatcher $event)
    {
        $this->event = $event;
    }


    /**
     * 门店订单验证支付成功之后，更改订单各个状态
     * @param $order
     */
    public function completeOrder($order)
    {
        $now = Carbon::now();
        $order->status = Order::STATUS_COMPLETE;
        $order->distribution_status = 1;
        $order->accept_time = $now;
        $order->send_time = $now;
        $order->completion_time = $now;
        $order->save();

        if (!ShopOrder::where('order_id', $order->id)->first()) {
            $ratio = settings('o2o_clerk_royalty_ratio') ? settings('o2o_clerk_royalty_ratio') / 100 : 0;
            $value = $order->total * $ratio;
            ShopOrder::create([
                'order_id' => $order->id,
                'clerk_id' => auth('clerk')->user()->id,
                'shop_id' => auth('clerk')->user()->shop_id,
                'value' => $value
            ]);
        };

        /*生效积分*/
        $this->event->fire('shop.order.completed', [$order]);
    }


}